
package com.sgcc.fj.pmos.common.view;

import android.content.Context;
import android.graphics.Color;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.github.mikephil.charting.data.CandleEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.utils.MPPointF;
import com.sgcc.pmos.nativesdk.R;
import com.sgcc.fj.pmos.common.adapter.ChartMakerAdapter;
import com.sgcc.fj.pmos.common.base.ChartMakerBean;
import com.sgcc.fj.pmos.common.utils.BigDecimalUtils;

import java.util.ArrayList;
import java.util.List;

public class TwoLineMarkerView extends com.github.mikephil.charting.components.MarkerView {

    private final TextView tvContent;
    private final RecyclerView rvList;
    private String[] xText;
    private List<List<String>> dataList;
    private Context context;
    private int[] colors1 = new int[]{Color.parseColor("#0092FF"),
            Color.parseColor("#FFA500")};

    public TwoLineMarkerView(Context context, String[] xText, List<List<String>> dataList) {
        super(context, R.layout.custom_marker_view1);
        this.context = context;
        this.xText = xText;
        this.dataList = dataList;
        tvContent = findViewById(R.id.tvContent);
        rvList = findViewById(R.id.rvList);
    }

    @Override
    public void refreshContent(Entry e, Highlight highlight) {
        if (e instanceof CandleEntry) {
            CandleEntry ce = (CandleEntry) e;
            setRvList((int) ce.getX());
            tvContent.setText("时段" + xText[(int) ce.getX()]);
        } else {
            setRvList((int) e.getX());
            tvContent.setText("时段" + xText[(int) e.getX()]);
        }
        super.refreshContent(e, highlight);
    }

    private void setRvList(int index) {
        List<ChartMakerBean> chartMakerBeanList = new ArrayList<>();
        for (int i = 0; i < dataList.size(); i++) {
            String value = BigDecimalUtils.onKeep2Decimal_new(dataList.get(i).get(index)) + "元/度";
            chartMakerBeanList.add(new ChartMakerBean(colors1[i], value));
        }
        rvList.setLayoutManager(new LinearLayoutManager(context));
        rvList.setAdapter(new ChartMakerAdapter(chartMakerBeanList));
    }


    @Override
    public MPPointF getOffset() {
        return new MPPointF(-(getWidth() / 2), -getHeight());
    }
}
